﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 相片明细 数据类
    /// </summary>
    public class AlbumEntry
    {
        public AlbumEntry()
        {
        }
        /// <summary>
        /// 获取某个相册图片数量
        /// <creator>wangl</creator>
        /// <date>2014-10-13</date>
        /// </summary>
        /// <param name="albumId"></param>
        /// <returns></returns>
        public int GetCountByFAlbumId(int albumId)
        {
            string strSql = "select count(*) from t_AlbumEntry where FAlbumId=@FAlbumId";
            SqlParameter[] parameters = { 
                    new SqlParameter("@FAlbumId", SqlDbType.Int) };

            parameters[0].Value = albumId;

            object obj = DbHelperSQL.GetSingle(strSql, parameters);
            if (null != obj)
            {
                return int.Parse(obj.ToString());
            }
            else
            {
                return 0;
            }

        }
        /// <summary>
        /// 获取最大显示顺序
        /// <creator>wangl</creator>
        /// <date>2014-10-13</date>
        /// </summary>
        /// <param name="docID"></param>
        /// <returns></returns>
        public int GetMaxDispSeq(int albumId)
        {
            string strSql = "select max(FDispSeq) from t_AlbumEntry where FAlbumId=@FAlbumId";
            SqlParameter[] parameters = { 
                    new SqlParameter("@FAlbumId", SqlDbType.Int) };
            parameters[0].Value = albumId;

            object obj = DbHelperSQL.GetSingle(strSql, parameters);
            if (null != obj)
            {
                return int.Parse(obj.ToString());
            }
            else
            {
                return 0;
            }

        }
        /// <summary>
        /// 增加一条数据
        /// <creator>wangl</creator>
        /// <date>2014-10-13</date>
        /// </summary>
        public decimal Add(Model.AlbumEntry model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_AlbumEntry(");
            strSql.Append("FAlbumId,FAlbumNote,FDispSeq,FFileType,FAlbumPhotoURL,FAlbumThumbURL)");
            strSql.Append(" values (");
            strSql.Append("@FAlbumId,@FAlbumNote,@FDispSeq,@FFileType,@FAlbumPhotoURL,@FAlbumThumbURL)");
            strSql.Append(";select IDENT_CURRENT('t_AlbumEntry')");
            SqlParameter[] parameters = {
					new SqlParameter("@FAlbumId", SqlDbType.Int,4),
					new SqlParameter("@FAlbumNote", SqlDbType.NVarChar,250),
					new SqlParameter("@FDispSeq", SqlDbType.Int,4),
					new SqlParameter("@FFileType", SqlDbType.NVarChar,200),
					new SqlParameter("@FAlbumPhotoURL", SqlDbType.NVarChar,255),
					new SqlParameter("@FAlbumThumbURL", SqlDbType.NVarChar,255)};
            parameters[0].Value = model.FAlbumId;
            parameters[1].Value = model.FAlbumNote;
            parameters[2].Value = model.FDispSeq;
            parameters[3].Value = model.FFileType;
            parameters[4].Value = model.FAlbumPhotoURL;
            parameters[5].Value = model.FAlbumThumbURL;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(obj);
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(decimal FAlbumEntryID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_AlbumEntry ");
            strSql.Append(" where FAlbumEntryID=@FAlbumEntryID");
            SqlParameter[] parameters = {
					new SqlParameter("@FAlbumEntryID", SqlDbType.Decimal)
};
            parameters[0].Value = FAlbumEntryID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// <creator>wangl</creator>
        /// <date>2014-10-14</date>
        /// </summary>
        /// <param name="entryIDs"></param>
        public void DelMany(string entryIDs)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_AlbumEntry where FAlbumEntryID in(");
            strSql.Append(entryIDs);
            strSql.Append(")");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }
        /// <summary>
        /// 得到一个对象实体
        /// <creator>wangl</creator>
        /// <date>2014-10-14</date>
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.AlbumEntry GetModel(decimal albumEntryID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FAlbumEntryID,FAlbumId,FAlbumNote,FFileType,FDispSeq,FAlbumPhotoURL,FAlbumThumbURL from t_AlbumEntry ");
            strSql.Append(" where FAlbumEntryID=@FAlbumEntryID");
            SqlParameter[] parameters = {
					new SqlParameter("@FAlbumEntryID", SqlDbType.Decimal)
			};
            parameters[0].Value = albumEntryID;

            ElingOA6_Admin_DSJCJ.Model.AlbumEntry model = new ElingOA6_Admin_DSJCJ.Model.AlbumEntry();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获取数据列表
        /// <creator>wangl</creator>
        /// <date>2014-10-14</date>
        /// </summary>
        /// <param name="docId"></param>
        /// <returns></returns>
        public DataSet GetListByAlbumId(int albumId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FAlbumId,FAlbumNote,FDispSeq,FFileType,FAlbumPhotoURL,FAlbumThumbURL,FAlbumEntryID ");
            strSql.Append(" FROM t_AlbumEntry where FAlbumId=@FAlbumId");
            SqlParameter[] parameters = { 
                    new SqlParameter("@FAlbumId", SqlDbType.Int) };
            parameters[0].Value = albumId;

            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        
        /// <summary>
        /// 更新一条数据
        /// <creator>wangl</creator>
        /// <date>2014-10-14</date>
        /// </summary>
        public bool Update(Model.AlbumEntry model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_AlbumEntry set ");
            strSql.Append("FAlbumId=@FAlbumId,");
            strSql.Append("FAlbumNote=@FAlbumNote,");
            strSql.Append("FDispSeq=@FDispSeq,");
            strSql.Append("FFileType=@FFileType,");
            strSql.Append("FAlbumPhotoURL=@FAlbumPhotoURL,");
            strSql.Append("FAlbumThumbURL=@FAlbumThumbURL");
            strSql.Append(" where FAlbumEntryID=@FAlbumEntryID");
            SqlParameter[] parameters = {
					new SqlParameter("@FAlbumId", SqlDbType.Int,4),
					new SqlParameter("@FAlbumNote", SqlDbType.NVarChar,250),
					new SqlParameter("@FDispSeq", SqlDbType.Int,4),
					new SqlParameter("@FFileType", SqlDbType.NVarChar,200),
					new SqlParameter("@FAlbumPhotoURL", SqlDbType.NVarChar,255),
					new SqlParameter("@FAlbumThumbURL", SqlDbType.NVarChar,255),
					new SqlParameter("@FAlbumEntryID", SqlDbType.Decimal,9)};
            parameters[0].Value = model.FAlbumId;
            parameters[1].Value = model.FAlbumNote;
            parameters[2].Value = model.FDispSeq;
            parameters[3].Value = model.FFileType;
            parameters[4].Value = model.FAlbumPhotoURL;
            parameters[5].Value = model.FAlbumThumbURL;
            parameters[6].Value = model.FAlbumEntryID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// <creator>wangl</creator>
        /// <date>2014-10-13</date>
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.AlbumEntry DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.AlbumEntry model = new ElingOA6_Admin_DSJCJ.Model.AlbumEntry();
            if (row != null)
            {
                if (row["FAlbumEntryID"] != null && row["FAlbumEntryID"].ToString() != "")
                {
                    model.FAlbumEntryID = decimal.Parse(row["FAlbumEntryID"].ToString());
                }
                if (row["FAlbumId"] != null && row["FAlbumId"].ToString() != "")
                {
                    model.FAlbumId = int.Parse(row["FAlbumId"].ToString());
                }
                if (row["FAlbumNote"] != null)
                {
                    model.FAlbumNote = row["FAlbumNote"].ToString();
                }
                if (row["FFileType"] != null)
                {
                    model.FFileType = row["FFileType"].ToString();
                }
                if (row["FDispSeq"] != null && row["FDispSeq"].ToString() != "")
                {
                    model.FDispSeq = int.Parse(row["FDispSeq"].ToString());
                }
                if (row["FAlbumPhotoURL"] != null)
                {
                    model.FAlbumPhotoURL = row["FAlbumPhotoURL"].ToString();
                }
                if (row["FAlbumThumbURL"] != null)
                {
                    model.FAlbumThumbURL = row["FAlbumThumbURL"].ToString();
                }
            }
            return model;
        }

    }
}
